/*
 *
 * SetupDialog.java
 *
 * Created on 10 de noviembre de 2008, 07:07 PM
 */

package thermologic.gui;

import java.awt.CardLayout;
import javax.swing.JOptionPane;
import thermologic.core.Adapter;
import thermologic.core.AdapterFactory;
import thermologic.core.Channel;
import thermologic.core.Datalogger;
import thermologic.core.DeviceSetupThread;
import thermologic.gui.SetupStep;

/**
 *
 * @author  mrivadeneira
 */
public class SetupDialog extends javax.swing.JDialog
{

    /**
     * Reference to main form
     */
    private MainForm mainForm;    
    
    /**
     * Datalogger object is going to configure
     */
    private Datalogger datalogger;
    
    /**
     * Array of tabs for each channel
     */
    private ChannelTab channelTabs[] = null;
    

    public SetupDialog(MainForm mainForm) {
        this(mainForm, true);
        this.mainForm = mainForm;
        
    }
    
    
    /** Creates new form SetupDialog */
    public SetupDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initCustomComponents();
        initComponents();
    }    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        startOptionsGroup = new javax.swing.ButtonGroup();
        panelStep1 = new javax.swing.JPanel();
        borderPanel = new javax.swing.JPanel();
        listScroll = new javax.swing.JScrollPane();
        deviceList = new javax.swing.JList();
        acceptButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        panelStep2 = new javax.swing.JPanel();
        headerPanel = new javax.swing.JPanel();
        deviceLabel = new javax.swing.JLabel();
        deviceId = new javax.swing.JTextField();
        clockSynch = new javax.swing.JCheckBox();
        startNow = new javax.swing.JRadioButton();
        startLater = new javax.swing.JRadioButton();
        delayInMinutes = new javax.swing.JSpinner();
        delayLabel = new javax.swing.JLabel();
        acceptButton2 = new javax.swing.JButton();
        cancelButton2 = new javax.swing.JButton();
        channelTabPane = new javax.swing.JTabbedPane();
        statusLabel = new javax.swing.JLabel();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("properties/language"); // NOI18N
        setTitle(bundle.getString("thermologic.setupdialog.title")); // NOI18N
        setLocationByPlatform(true);
        setResizable(false);
        getContentPane().setLayout(new java.awt.CardLayout());

        borderPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder()));

        deviceList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        listScroll.setViewportView(deviceList);

        javax.swing.GroupLayout borderPanelLayout = new javax.swing.GroupLayout(borderPanel);
        borderPanel.setLayout(borderPanelLayout);
        borderPanelLayout.setHorizontalGroup(
            borderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(borderPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(listScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE)
                .addContainerGap())
        );
        borderPanelLayout.setVerticalGroup(
            borderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(borderPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(listScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
                .addContainerGap())
        );

        acceptButton.setText(bundle.getString("thermologic.setupdialog.acceptButton")); // NOI18N
        acceptButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                acceptButtonActionPerformed(evt);
            }
        });

        cancelButton.setText(bundle.getString("thermologic.setupdialog.cancelButton")); // NOI18N

        javax.swing.GroupLayout panelStep1Layout = new javax.swing.GroupLayout(panelStep1);
        panelStep1.setLayout(panelStep1Layout);
        panelStep1Layout.setHorizontalGroup(
            panelStep1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelStep1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelStep1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(borderPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(panelStep1Layout.createSequentialGroup()
                        .addComponent(acceptButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancelButton)))
                .addContainerGap())
        );
        panelStep1Layout.setVerticalGroup(
            panelStep1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelStep1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(borderPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(panelStep1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(acceptButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        getContentPane().add(panelStep1, "STEP1");

        headerPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder()));

        deviceLabel.setText(bundle.getString("thermologic.traceTable.param1")); // NOI18N

        deviceId.setEnabled(false);

        clockSynch.setText(bundle.getString("thermologic.setupdialog.syncclock")); // NOI18N

        startOptionsGroup.add(startNow);
        startNow.setText(bundle.getString("thermologic.setupdialog.startNow")); // NOI18N

        startOptionsGroup.add(startLater);
        startLater.setText(bundle.getString("thermologic.setupdialog.startLater")); // NOI18N

        delayLabel.setText(bundle.getString("thermologic.setupdialog.timeDelayLabel")); // NOI18N

        javax.swing.GroupLayout headerPanelLayout = new javax.swing.GroupLayout(headerPanel);
        headerPanel.setLayout(headerPanelLayout);
        headerPanelLayout.setHorizontalGroup(
            headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(headerPanelLayout.createSequentialGroup()
                .addGroup(headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(startNow)
                    .addGroup(headerPanelLayout.createSequentialGroup()
                        .addComponent(startLater)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 110, Short.MAX_VALUE)
                        .addComponent(delayLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(delayInMinutes, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(clockSynch)
                    .addGroup(headerPanelLayout.createSequentialGroup()
                        .addComponent(deviceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deviceId, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        headerPanelLayout.setVerticalGroup(
            headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(headerPanelLayout.createSequentialGroup()
                .addGroup(headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(deviceLabel)
                    .addComponent(deviceId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addComponent(clockSynch)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(startNow)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(startLater)
                    .addComponent(delayLabel)
                    .addComponent(delayInMinutes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        acceptButton2.setText(bundle.getString("thermologic.setupdialog.acceptButton")); // NOI18N
        acceptButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                acceptButton2ActionPerformed(evt);
            }
        });

        cancelButton2.setText(bundle.getString("thermologic.setupdialog.cancelButton")); // NOI18N

        javax.swing.GroupLayout panelStep2Layout = new javax.swing.GroupLayout(panelStep2);
        panelStep2.setLayout(panelStep2Layout);
        panelStep2Layout.setHorizontalGroup(
            panelStep2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelStep2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelStep2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(channelTabPane, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE)
                    .addComponent(headerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(panelStep2Layout.createSequentialGroup()
                        .addComponent(acceptButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(statusLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)))
                .addContainerGap())
        );
        panelStep2Layout.setVerticalGroup(
            panelStep2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelStep2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(headerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(channelTabPane, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelStep2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(acceptButton2)
                    .addComponent(cancelButton2)
                    .addComponent(statusLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(panelStep2, "STEP2");

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void acceptButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acceptButton2ActionPerformed
    save();
}//GEN-LAST:event_acceptButton2ActionPerformed

private void acceptButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acceptButtonActionPerformed

   datalogger = (Datalogger)deviceList.getSelectedValue();
   if(datalogger != null)
   {
       show(SetupStep.STEP2, datalogger);
   }
}//GEN-LAST:event_acceptButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton acceptButton;
    private javax.swing.JButton acceptButton2;
    private javax.swing.JPanel borderPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton cancelButton2;
    private javax.swing.JTabbedPane channelTabPane;
    private javax.swing.JCheckBox clockSynch;
    private javax.swing.JSpinner delayInMinutes;
    private javax.swing.JLabel delayLabel;
    private javax.swing.JTextField deviceId;
    private javax.swing.JLabel deviceLabel;
    private javax.swing.JList deviceList;
    private javax.swing.JPanel headerPanel;
    private javax.swing.JScrollPane listScroll;
    private javax.swing.JPanel panelStep1;
    private javax.swing.JPanel panelStep2;
    private javax.swing.JRadioButton startLater;
    private javax.swing.JRadioButton startNow;
    private javax.swing.ButtonGroup startOptionsGroup;
    private javax.swing.JLabel statusLabel;
    // End of variables declaration//GEN-END:variables

    
    private void initCustomComponents()
    {
    }
    
    private void setFormData(Datalogger datalogger)
    {   
        deviceId.setText(datalogger.getId());
        statusLabel.setText("");
        
        channelTabs = null;
        channelTabs = new ChannelTab[datalogger.getChannels().length];
        
        channelTabPane.removeAll();
        int i=0;
        
        for(Channel channel: datalogger.getChannels())
        {
            channelTabs[i] = new ChannelTab();
            channelTabs[i].setChannel(channel);
            
            channelTabPane.addTab(channel.getLabel(),channelTabs[i]);
            i++;
        }
    }
    
    public void show(SetupStep step, Datalogger d)
    {
        CardLayout layout = (CardLayout)getContentPane().getLayout();
        datalogger = d;


        switch(step)
        {
            case STEP1: 
                deviceList.setListData(AdapterFactory.getAdapter().getDevices());
                layout.show(this.getContentPane(), step.name());
                break;

            case STEP2:
                setFormData(datalogger);
                layout.show(this.getContentPane(), step.name());
        }

        setVisible(true);
    }
    
    
    public void save()
    {
        /**
         * Update the datalogger object from values
         * of UI components
         */
        datalogger.setSynchronizeWithClock(clockSynch.isSelected());
        for(ChannelTab channelTab: channelTabs)
        {
            channelTab.updateChannel();
        }
        
        /**
         * Launch the setup thread
         */
        Adapter  adapter = AdapterFactory.getAdapter();
        if (!adapter.isBussy())
        {
            Thread thread = new Thread(new DeviceSetupThread(adapter, datalogger));
            thread.start();
        }
    }   
}
